import os   
import platform
CPPPATH=[]
sources=[]
env=DefaultEnvironment().Clone()
OS_NAME=os.environ['TARGET_OS'];
LIB_DIR=os.environ['LIB_DIR'];

SDL_FLAGS = ' -DSDL_REAL_API -DSDL_HAPTIC_DISABLED -DSDL_SENSOR_DISABLED -DSDL_JOYSTICK_DISABLED '

if OS_NAME == 'Windows':
  CPPPATH=['include','src', 'src/video/khronos']
  sources=Glob('src/atomic/*.c')+\
    Glob('src/audio/directsound/*.c')+\
    Glob('src/audio/disk/*.c')+\
    Glob('src/audio/dummy/*.c')+\
    Glob('src/audio/*.c')+\
    Glob('src/audio/winmm/*.c')+\
    Glob('src/audio/wasapi/*.c')+\
    Glob('src/core/windows/*.c')+\
    Glob('src/cpuinfo/*.c')+\
    Glob('src/events/*.c')+\
    Glob('src/file/*.c')+\
    Glob('src/filesystem/windows/*.c')+\
    Glob('src/libm/*.c')+\
    Glob('src/loadso/windows/*.c')+\
    Glob('src/power/*.c')+\
    Glob('src/power/windows/*.c')+\
    Glob('src/render/direct3d/*.c')+\
    Glob('src/render/direct3d11/*.c')+\
    Glob('src/render/opengl/*.c')+\
    Glob('src/render/opengles2/*.c')+\
    Glob('src/render/software/*.c')+\
    Glob('src/render/*.c')+\
    Glob('src/stdlib/*.c')+\
    Glob('src/thread/generic/SDL_syscond.c')+\
    Glob('src/thread/SDL_thread.c')+\
    Glob('src/thread/windows/*.c')+\
    Glob('src/timer/SDL_timer.c')+\
    Glob('src/timer/windows/*.c')+\
    Glob('src/video/dummy/*.c')+\
    Glob('src/video/windows/*.c')+\
    Glob('src/video/*.c')+\
    Glob('src/video/yuv2rgb/*.c')+\
    Glob('src/*.c')
    
elif OS_NAME == 'Linux':
  CPPPATH=[
    '/usr/include/gtk-3.0',
    '/usr/include/dbus-1.0',
    '/usr/lib/x86_64-linux-gnu/dbus-1.0/include',
    '/usr/include/gio-unix-2.0/',
    '/usr/include/pango-1.0',
    '/usr/include/atk-1.0',
    '/usr/include/cairo',
    '/usr/include/pixman-1',
    '/usr/include/gdk-pixbuf-2.0',
    '/usr/include/glib-2.0',
    '/usr/lib/glib-2.0/include',
    '/usr/lib/i386-linux-gnu/glib-2.0/include',
    '/usr/lib/arm-linux-gnueabihf/glib-2.0/include',
    '/usr/lib/x86_64-linux-gnu/glib-2.0/include',
    '/usr/lib/aarch64-linux-gnu/glib-2.0/include',
    '/usr/include/ibus-1.0',
    'include',
    '/usr/include/harfbuzz',
    'gen',
    'src/video/khronos',
    'src/audio/alsa',
    'src'
    ]
  sources=Glob('gen/*.c')+\
  Glob("src/*.c")+\
  Glob('src/atomic/*.c')+\
  Glob('src/audio/*.c')+\
  Glob('src/cpuinfo/*.c')+\
  Glob('src/events/*.c')+\
  Glob('src/file/*.c')+\
  Glob('src/libm/*.c')+\
  Glob('src/power/*.c')+\
  Glob('src/render/opengl/*.c')+\
  Glob('src/render/opengles2/*.c')+\
  Glob('src/render/software/*.c')+\
  Glob('src/render/*.c')+\
  Glob('src/stdlib/*.c')+\
  Glob('src/thread/*.c')+\
  Glob('src/timer/SDL_timer.c')+\
  Glob('src/video/yuv2rgb/*.c')+\
  Glob('src/video/*.c')+\
  Glob('src/loadso/dlopen//*.c')+\
  Glob('src/audio/dummy/*.c')+\
  Glob('src/audio/sndio/*.c')+\
  Glob('src/video/dummy/*.c')+\
  Glob('src/audio/alsa/*.c')+\
  Glob('src/core/linux/*.c')+\
  Glob('src/thread/pthread/*.c')+\
  Glob('src/power/linux/*.c')+\
  Glob('src/filesystem/unix/*.c')+\
  Glob('src/timer/unix/*.c')+\
  Glob('src/core/unix/*.c')+\
  Glob('src/main/dummy/*.c')

  SDL_FLAGS += ' -DSDL_TIMER_UNIX  -DSDL_AUDIO_DRIVER_ALSA -DSDL_LOADSO_DLOPEN '
  SDL_FLAGS += ' -DSDL_VIDEO_OPENGL_EGL -DSDL_VIDEO_OPENGL_ES2 '

  SDL_VIDEODRIVER=os.getenv('SDL_VIDEODRIVER');
  if SDL_VIDEODRIVER is None:
    SDL_VIDEODRIVER = 'x11'

  if SDL_VIDEODRIVER == 'x11' or SDL_VIDEODRIVER == 'wayland':
    sources += Glob('src/video/'+SDL_VIDEODRIVER+'/*.c')
  else:
    SDL_VIDEODRIVER = 'x11'
    sources += Glob('src/video/'+SDL_VIDEODRIVER+'/*.c')

  if SDL_VIDEODRIVER == 'x11':
    SDL_FLAGS += ' -DSDL_VIDEO_DRIVER_X11 -DSDL_VIDEO_DRIVER_X11_SUPPORTS_GENERIC_EVENTS '
    SDL_FLAGS += ' -DSDL_VIDEO_OPENGL_GLX -DSDL_VIDEO_RENDER_OGL '
  elif SDL_VIDEODRIVER == 'wayland':
    SDL_FLAGS = SDL_FLAGS + ' -DSDL_VIDEO_DRIVER_WAYLAND -DWITHOUT_NATIVE_FILE_DIALOG '
      
CCFLAGS=os.environ['CCFLAGS'] + SDL_FLAGS;
CCFLAGS = CCFLAGS + ' -DSDL_STATIC_LIB -D__FLTUSED__ '

if OS_NAME == 'Linux':
  if os.environ['SDL_UBUNTU_USE_IME'] == str(True) :
    CCFLAGS = CCFLAGS + ' -DSDL_USE_IME -DHAVE_FCITX_FRONTEND_H -DHAVE_IBUS_IBUS_H -DHAVE_DBUS_DBUS_H -DHAVE_GETENV '

env.Library(os.path.join(LIB_DIR, 'SDL2'), sources, CPPPATH = CPPPATH, CCFLAGS = CCFLAGS)

